<?php
/*
 *账单收益
*/
namespace App\Service;

use App\Models\EwBill;
use App\Models\EwMember;

class BillService extends BaseService
{
	const STATE_PAYED = 2;
	const STATE_NOTPAY = 1;
	const WEB = "觅屋·媒体人";

	/**
	 * [getList 获取账单收益列表]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function getList($params, $count = FALSE)
	{
		$where['limit'] = $params->page_num;
		$where['offset'] = ($params->p - 1)*$params->page_num;

		if (isset($params->member_id) && $params->member_id){
			$builder = new \Phalcon\Mvc\Model\Query\Builder();
			$builder->columns(['b.id,b.member_id,b.to_member_id,m.nickname,b.data_id,d.thumb,d.doc,b.bill_name,b.bn,b.amount,b.pay_time,b.pay_code,b.bill_type,b.create_time,"' . self::WEB . '" as platform']);
			$builder->addFrom("App\Models\EwBill", 'b');

			$builder->leftJoin("App\Models\EwData", 'b.data_id = d.id' , 'd');

			if ($params->pay == 0){//收益 pay = 0 bill_type = 1 member_id
				$builder->leftJoin("App\Models\EwMember", 'b.member_id = m.id' , 'm');
				$builder->where("b.to_member_id = " . $params->member_id);
			}else if ($params->pay == 1){//账单 pay = 1 bill_type = 1 to_member_id
				$builder->andWhere("b.bill_type = 1");
				$builder->leftJoin("App\Models\EwMember", 'b.to_member_id = m.id' , 'm');
				$builder->where("b.member_id = " . $params->member_id);
				$builder->andWhere("b.bill_type = 1");
			}else if($params->pay == 2){//付费数据 pay = 2 bill_type = 2 member_id
				$builder->leftJoin("App\Models\EwMember", 'b.member_id = m.id' , 'm');
				$builder->where("b.member_id = " . $params->member_id);
				$builder->andWhere("b.bill_type = 2");
			}
			if ($count)
				return $builder->getQuery()->execute()->count();

			$builder->limit($params->page_num,($params->p-1)*$params->page_num);
			$result = $builder->getQuery();

			return $result->execute();

		}else{
			return NULL;
		}
	}

	/**
	 * [getInfo 获取详情]
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	public static function getInfo($id = 0)
	{
		if (EwBill::findFirst($id)){
			$data = EwBill::findFirst($id)->toArray();

			$data['member_nickname'] = NULL;
			$data['to_member_nickname'] = NULL;
			$data['data_title'] = NULL;
			//打赏人
			if ($data['member_id']){
				$member = EwMember::findFirst($data['member_id']);
				$data['member_nickname'] = $member->nickname;
			}
			//被打赏人
			if ($data['to_member_id']){
				$member = EwMember::findFirst($data['to_member_id']);
				$data['to_member_nickname'] = $member->nickname;
			}
			//数据标题
			if ($data['data_id']) {
				$Data = EwData::findFirst($data['data_id']);
				$data['data_title']	= $Data->title;
			}
			return $data;
		}

		return NULL;
	}

	/**
	 * [count 获取总数量]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function count($params)
	{
		$query = EwBill::query()->columns(['count(id) as count']);

		if (isset($params->member_id) && $params->member_id)
			$query->andWhere("member_id = ". $params->member_id);

		$data = $query->execute()->toArray();

		if (isset($data[0]['count']))
			return $data[0]['count'];

		return NULL;
	}


	public static function getSearch($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns(['DISTINCT(b.id) AS id,b.member_id,m.nickname AS pay_member,b.to_member_id,mb.nickname AS income_member,b.data_id,d.title,b.bill_name,b.bn,b.amount,b.pay_time,b.pay_code,b.bill_type,b.create_time']);
		$builder->addFrom("App\Models\EwBill", 'b');
		$builder->leftJoin("App\Models\EwMember", 'b.member_id = m.id' , 'm');
		$builder->leftJoin("App\Models\EwMember", 'b.to_member_id = mb.id' , 'mb');
		$builder->leftJoin("App\Models\EwData", 'b.data_id = d.id' , 'd');

		//bill_type
		if (isset($params->bill_type) && $params->bill_type > 0)
			$builder->andWhere("b.bill_type = " . $params->bill_type);

		//min_amount
		if (isset($params->min_amount) && $params->min_amount)
			$builder->andwhere('b.amount >= ' . $params->min_amount);

		//max_amount
		if (isset($params->max_amount) && $params->max_amount)
			$builder->andwhere('b.amount <= ' . $params->max_amount);

		//nickname pay
		if (isset($params->nickname) && $params->nickname){
			if (isset($params->bill_type) && $params->bill_type == 1){//打赏
				$builder->andWhere('m.nickname LIKE "%' . $params->nickname . '%"');
			}else if(isset($params->bill_type) && $params->bill_type == 0){//被打赏
				$builder->andWhere('mb.nickname LIKE "%' . $params->nickname . '%"');
			}else{//所有
				$builder->andWhere('m.nickname LIKE "%' . $params->nickname . '%" OR mb.nickname LIKE "%' . $params->nickname . '%"');
			}
		}

		//pay_time
		if (isset($params->start_time) && isset($params->end_time) && $params->start_time && $params->end_time)
			$builder->andwhere('b.pay_time BETWEEN ' . $params->start_time . ' AND ' . $params->end_time);

		//bn
		if (isset($params->bn) && $params->bn)
			$builder->andWhere('b.bn LIKE "%' . $params->bn . '%"');

		//title
		if (isset($params->title) && $params->title)
			$builder->andWhere('d.title LIKE "%' . $params->title . '%"');

		//bill_name
		if (isset($params->bill_name) && $params->bill_name)
			$builder->andWhere('b.bill_name LIKE "%' . $params->bill_name . '%"');

		//pay_code
		if (isset($params->pay_code) && $params->pay_code)
			$builder->andWhere('b.pay_code LIKE "%' . $params->pay_code . '%"');

		if ($count){
			return $builder->getQuery()->execute()->count();
		}else{
			$builder->limit($params->page_num,($params->p-1)*$params->page_num);
			$result = $builder->getQuery();
			return $result->execute();
		}
	}

	//保存
  public static function save($data){

      $bill = new EwBill;

			return $bill->save((array)$data);
	}

	public static function getBnInfo($bn)
	{
			return EwBill::findFirstByBn($bn);
	}

}